<?php

	// web_site()显示第二个子菜单位置管理的页面内容
	function web_site() {
		
		global $wpdb;
		$deltoken = $_GET["deltoken"]; 
		$template_id = $_POST["template_id"];
 
		// $deltoken等于1为删除位置信息，$deltoken等于2为修改信息$deltoken等于三为添加
		
		//添加位置处理		
	    if($deltoken == "1"){
			$site_id_delete = $_GET["site_id_delete"];
			$template_id = $_GET["template_id"];
			
			$sqlquery = "delete from  bw_template_site where id=$site_id_delete";
			$wpdb->query($sqlquery);
			$wpdb->flush();
			
		//修改位置信息  
		}else if($deltoken == "2"){
			$site_id_update = $_GET["site_id_update"];  
			$sqlquery = "select * from bw_template_site where id ='$site_id_update'"; 
			$update_site = $wpdb->get_results($sqlquery);
		}
		
	if(empty($deltoken) && !empty($_POST["deltoken"])){
            $deltoken = $_POST["deltoken"];
			if($deltoken == "5"){
 				handAddSite($_POST,$_GET);
			   //删除位置信息
			}else if($deltoken == "4"){
 				handModSite($_POST,$_GET); 
			} 
			
	 }else if(!empty($deltoken) && empty($template_id)){
	 	$template_id = $_GET["template_id"];
	 	
	 }
		
		
	  	//对数据表bw_template的sql查询语句
		$sql_template ="select id,name from bw_template";
		$sql_site ="";
		if(strlen($template_id)==0){
			$template_id = "";
			$sql_site ="select * from bw_template_site";
		}else{
			$sql_site ="select * from bw_template_site where template_id='$template_id'";
		}
		//对数据表bw_template_site的sql查询语句
		$sql_site ="select * from bw_template_site where template_id='$template_id'";

		$result_template = $wpdb->get_results($sql_template);
		$result_site = $wpdb->get_results($sql_site);
		
	    echo '<h2>位置管理</h2>';
	    echo '<span>在下面选框首先选出相应的模板 </span>';
	    echo '<form id="static_web" method="post" action="?page=sub-page2" ><select name="template_id">
	    <option value="请点击选择模板" selected="selected" >请点击选择模板</option>';
		foreach($result_template as $row_tem){
			
			 if($template_id == $row_tem->id){	
			 	$tempid = $row_tem->id;
			 	echo'<option value="'.$row_tem->id.'" selected="selected">'.$row_tem->name.'</option>';
			 }else{
	    		echo'<option value="'.$row_tem->id.'">'.$row_tem->name.'</option>';
			 }
	    }
	    echo '<input type="submit" value="确定" class="button-secondary action" />
	    	  <input type="button" value="添加新位置" onclick="javascript:window.location.href=\'?page=sub-page2&deltoken=3&template_id='.$template_id.'\';"  class="button-secondary action" /> ';
		echo '</select>';
		echo'<table  cellspacing="0" class="wp-list-table widefat fixed posts">
		<thead>
			<th width="10%">位置名称</th>
			<th width="10%">位置描述</th>
			<th width="10%">显示数量</th>
			<th width="40%">位置HTML</th>
			<th width="5%">对应模板id</th>
			<th width="15%">操作</th>
		</thead>';
		//获得内容表的详细信息、位置名称name、位置描述content、次位置显示内容数量shownum
		foreach($result_site as $row_site){
			
			$sitehtml = htmlspecialchars($row_site->site_html);
			
			echo'<tr>
			<td>'.$row_site->name.'</td>
			<td>'.$row_site->content.'</td>
			<td>'.$row_site->shownum.'</td>
			<td>'.$sitehtml.'</td>
			<td>'.$row_site->template_id.'</td>
			<td>
			  <input type="button" value="修改" onclick="javascript:window.location.href=\'?page=sub-page2&deltoken=2&template_id='.$template_id.'&site_id_update='.$row_site->id.'\';"  class="button-secondary action" />
			   <input type="hidden" name="site_id_update"  value="'.$row_site->id.'" />
	          <input type="button" value="删除" onclick="javascript:window.location.href=\'?page=sub-page2&deltoken=1&template_id='.$template_id.'&site_id_delete='.$row_site->id.'\';"  class="button-secondary action" />
	          <input type="hidden" name="site_id_delete"  value="'.$row_site->id.'" />
			</td>
			</tr>';
		}
		echo '</table>';
		if($deltoken == "2"){
			foreach($update_site as $updaterow ){
				if($site_id_update == $updaterow->id ){
				   //得到串
				   echo getAddOrModFormStr($deltoken,$updaterow);
				}
			}
		}else if($deltoken == "3"){
			 echo getAddOrModFormStr($deltoken,$template_id);
		}
		
}
//当deltoken   3     显示新增位置的表单
function handAddSite($post,$get){ 
		global $wpdb;
		   $template_id = $post["template_id"];
			$site_name = $post["site_name"];
			$site_content = $post["site_content"];
			$site_shownum = $post["site_shownum"];
			$site_html = $post["site_html"];  
			$site_html = preg_replace("/\\\/",'',$site_html);
			$wpdb->insert('bw_template_site', array('name' => $site_name,'content' =>$site_content,'shownum' => $site_shownum,'template_id' =>$template_id,'site_html' => $site_html));
         
		 
			
}

//deltoken   4     执行修改位置的函数
function handModSite($post,$get){
		global $wpdb; 
		
		    $site_name = $post["site_name"];
			$site_content = $post["site_content"];
			$site_shownum = $post["site_shownum"];
			$site_html = $post["site_html"];  
			$site_html = preg_replace("/\\\/",'',$site_html);
			 $site_id_update = $post["site_id"];
			$wpdb->update('bw_template_site', array('name' => $site_name,'content' =>$site_content,'shownum' => $site_shownum,'site_html' => $site_html), array( 'id' =>$site_id_update ), array('%s', '%s','%d', '%s'), array('%d'));
 			echo $site_id_update."修改成功";
			
}
//得到删除或是修改表单的串
function getAddOrModFormStr($token,$updaterow){
		$str = '<div class="wrap">
					  <div class="icon32" id="icon-users">
						<br>
					  </div><h2 id="add-new-user">设置位置内容</h2>
						<div id="ajax-response"></div>
						<p>在相应的选框填入相应的修改值。</p>
						<table class="form-table">
							<tbody>
							<tr class="form-field form-required">
								<th scope="row">
									<label for="site_name">位置名称<span class="description">(必填)</span></label>
								</th>
								<td><input type="text" aria-required="true" value="'.$updaterow->name.'" id="site_name" name="site_name" /></td>
							</tr>
							<tr class="form-field form-required">
								<th scope="row">
									<label for="site_content">位置描述 <span class="description">(必填)</span></label>
								</th>
								<td><input type="text" value="'.$updaterow->content.'" id="site_content" name="site_content" /></td>
							</tr>
							<tr class="form-field">
								<th scope="row">
									<label for="site_shownum">显示数量 <span class="description">(必填)</span></label>
								</th>
								<td>
								<select id="site_shownum" name="site_shownum">
									<option>请选择文章显示数量</option>
									<option value="1" >1</option>
									<option value="2" >2</option>
									<option value="3" >3</option>
									<option value="4" >4</option>
									<option value="5" >5</option>
									<option value="6" >6</option>
									<option value="7" >7</option>
									<option value="8" >8</option>
									<option value="9" >9</option>
									<option value="10" >10</option>
									<option value="11" >11</option>
									<option value="12" >12</option>
									<option value="13" >13</option>
									<option value="14" >14</option>
									<option value="15" >15</option>
									<option value="16" >16</option>
									<option value="17" >17</option>
									<option value="18" >18</option>
									<option value="19" >19</option>
									<option value="20" >20</option>
									<option value="21" >21</option>
									<option value="22" >22</option>
									<option value="23" >23</option>
									<option value="24" >24</option>
									<option value="25" >25</option>
									<option value="26" >26</option>
									<option value="27" >27</option>
									<option value="28" >28</option>
									<option value="29" >29</option>
									<option value="30" >30</option>
									</select>
							</td>
						</tr>
						<tr class="form-field">
							<th scope="row">
								<label for="site_html">对应HTML<span class="description">(必填)</span></label>
							</th>
							<td><textarea id="site_html" name="site_html" style="width:420px;" >'.$updaterow->site_html.'</textarea>
							</td>
						</tr>	
					</tbody>
					</table>';	
		if($token == "3"){
						$str.='<input type="hidden" name="template_id" value="'.$updaterow.'"/>';
			
			$str.='<input type="hidden" name="deltoken" value="5" />';
			$str.='<p class="submit"><input type="submit" value="添加新位置" class="button-primary" id="createusersub" name="addsite"></p>';
		}else if($token == "2"){					
			$str.='<input type="hidden" name="template_id" value="'.$updaterow->template_id.'"/>';
		    $str.='<input type="hidden" name="deltoken" value="4" />';	 
   		     $str.='<input type="hidden" name="site_id" value="'.$updaterow->id.'" />';			
			 $str.='<p class="submit"><input type="submit" value="保存修改" class="button-primary" id="createusersub" name="addsite"></p>';
		}
		$str.='</form> </div>';
		return $str;
}
 
